﻿using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface.Iridgepletype;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Domain.Impl.Ridgepletype
{
    public class RidegRepository : IridegRepository
    {

        public readonly AgricultureDbContext _context;
        public RidegRepository(AgricultureDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 添加功能
        /// </summary>
        /// <param name="entity">添加实体</param>
        /// <returns></returns>
       
        public Task<int> Add(Ridgepole entity)
        {   try
            {
                entity.CreateTime = DateTime.Now;

                _context.Add(entity);
                return _context.SaveChangesAsync();
            }
            catch (Exception) { throw; }
        }

        IQueryable<Ridgepole> IridegRepository.Showinfo()
        {
            try
            {
                return _context.Ridgepoles.Where(x => x.IsDel == false);


            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 修改功能
        /// </summary>
        /// <param name="entity">修改实体</param>
        /// <returns></returns>
        public async Task<int> Update(Ridgepole entity)
        {
            try
            {
                _context.Entry(entity).State = EntityState.Modified;
                return await _context.SaveChangesAsync();
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 执行单个逻辑删除操作
        /// </summary>
        /// <param name="id">要逻辑删除的条目的唯一标识符</param>
        /// <returns>返回一个表示异步操作的任务，任务结果为受影响的行数</returns>
        public Task<int> Delete(int id)
        {
            try
            {
                var list = _context.Ridgepoles.FirstOrDefault(x => x.RidgepoleId == id);
                if (list != null)
                {
                    list.IsDel = true;
                    _context.Update(list);
                }
                return _context.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 执行批量逻辑删除操作
        /// </summary>
        /// <param name="ids">包含要逻辑删除的条目的唯一标识符集合</param>
        /// <returns>返回一个表示异步操作的任务，任务结果为受影响的行数</returns>
        public Task<int> DeleteRange(IEnumerable<int> ids)
        {
            try
            {
                foreach (var item in ids)
                {
                    var list = _context.Ridgepoles.FirstOrDefault(x => x.RidgepoleId == item);
                    if (list != null)
                    {
                        list.IsDel = true;
                        _context.Update(list);
                    }
                }
                return _context.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 栋舍反填功能
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>根据ID返回列表</returns>
        public async Task<Ridgepole> Find(int id)
        {
            try
            {
                return _context.Ridgepoles.FirstOrDefault(x => x.RidgepoleId == id);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
